//
//  ServiceTimePickerView.m
//  DingDongXiChe
//
//  Created by wtjr on 2017/11/28.
//  Copyright © 2017年 xieyingze. All rights reserved.
//

#import "ServiceTimePickerView.h"


@interface ServiceTimePickerView()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic,retain)UIPickerView *pickerview;

@property (nonatomic,retain)NSMutableArray *firstData,*secondData,*thirdData;

@property (nonatomic,assign)NSInteger hour,minute;

@property (nonatomic, assign) int selectedDay;

@property (nonatomic, assign) NSInteger selectRow1;

@property (nonatomic, assign) NSInteger selectRow2;

@property (nonatomic, assign) NSInteger selectRow3;


@end

@implementation ServiceTimePickerView


#pragma mark - BaseMethod

- (void)initData {
    self.firstData=[[NSMutableArray alloc]initWithObjects:@"今天",@"明天",@"后天", nil];
    
    NSDateComponents *dateComponent = [[NSCalendar currentCalendar] components:NSCalendarUnitHour|NSCalendarUnitMinute fromDate:[NSDate date]];
    self.hour = [dateComponent hour];
    self.minute = [dateComponent minute];
    [self dealWithHourse];
    [self dealWithSecond];
    
    self.selectedDay = 0;
    self.selectRow1 = self.selectRow2 = self.selectRow3 = 0;
}

- (void)initSubViews {
    [self addSubview:self.pickerview];
}

- (void)autoLayoutSubViews {
    [self.pickerview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];
    [self returnPickerViewSelectString:self.pickerview];
}

#pragma mark - Method

- (void)dealWithHourse {
    [self.secondData removeAllObjects];
    if (self.hour < 7) {
        [self.secondData addObject:@"服务时间未到"];
    } else if (self.hour >= 21) {
        [self.secondData addObject:@"服务时间已过"];
    } else {
        if (self.minute == 0) {
            for (NSInteger i = self.hour; i <= 22; i++) {
                [self.secondData addObject:[NSString stringWithFormat:@"%ld点",i]];
            }
        } else {
            for (NSInteger i = self.hour + 1; i <= 22; i++) {
                [self.secondData addObject:[NSString stringWithFormat:@"%ld点",i]];
            }
        }
    }
}

- (void)dealWithSecond {
    [self.thirdData removeAllObjects];
    if (self.minute == 0) {
        [self.thirdData addObjectsFromArray:@[@"0分",@"15分",@"30分",@"45分"]];
    } else if (self.minute > 0 && self.minute <= 15) {
        [self.thirdData addObjectsFromArray:@[@"15分",@"30分",@"45分"]];
    } else if (self.minute <= 30) {
        [self.thirdData addObjectsFromArray:@[@"30分",@"45分"]];
    } else if (self.minute <= 45) {
        [self.thirdData addObjectsFromArray:@[@"45分"]];
    } else {
        [self.thirdData addObjectsFromArray:@[@"0分",@"15分",@"30分",@"45分"]];
    }
}


#pragma mark - Interface

- (void)returnPickerViewSelectString:(UIPickerView *)pickerView {
    [self.pickerview reloadComponent:0];
    [self.pickerview reloadComponent:1];
    [self.pickerview reloadComponent:2];
    NSString *part1 = self.firstData[[pickerView selectedRowInComponent:0]];
    NSString *part2 = [self.secondData[[pickerView selectedRowInComponent:1]] stringByReplacingOccurrencesOfString:@"点" withString:@""];
    NSString *part3 = [self.thirdData[[pickerView selectedRowInComponent:2]] stringByReplacingOccurrencesOfString:@"分" withString:@""];
    NSString *selectTime =  [[NSString stringWithFormat:@"%@-%@-%@",part1,part2,part3] stringByReplacingOccurrencesOfString:@"-" withString:@":"];
    self.selectTime = selectTime;
    if (self.selectCompleted) {
        self.selectCompleted(selectTime);
    }
}

#pragma mark - Delegate

//返回显示的列数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}

//返回当前列显示的行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component==0) {
        return [self.firstData count];
    }else if (component==1){
        return [self.secondData count];
    }else{
        return [self.thirdData count];
    }
}

//设置滚轮的宽度
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if (component == 0) {
        return 100;
    }else if(component==1){
        return 100;
    }else{
        return 100;
    }
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    if (component == 0) {
        [pickerView selectRow:0 inComponent:1 animated:YES];
        [pickerView selectRow:0 inComponent:2 animated:YES];
        
        self.selectedDay = (int)row;
        [self.secondData removeAllObjects];
        [self.thirdData removeAllObjects];
        
        if (row > 0) {
            for (int i = 7; i < 23; i++) {
                [self.secondData addObject:[NSString stringWithFormat:@"%d点",i]];
            }
            [self.thirdData addObjectsFromArray:@[@"0分",@"15分",@"30分",@"45分"]];
        } else {
            [self dealWithHourse];
            [self dealWithSecond];
        }
        //设置选中行数
        self.selectRow1 = row;
        self.selectRow2 = self.selectRow3 = 0;
    } else if (component == 1) {
        [pickerView selectRow:0 inComponent:2 animated:YES];
        
        if (self.secondData.count <=1 ) {
            return;
        }
        if (row > 0 && row < self.secondData.count - 1) {
            [self.thirdData removeAllObjects];
            [self.thirdData addObjectsFromArray:@[@"0分",@"15分",@"30分",@"45分"]];
        } else if (row == self.secondData.count - 1) {
            [self.thirdData removeAllObjects];
            [self.thirdData addObject:@"0分"];
        } else {
            [self.thirdData removeAllObjects];
            if (self.selectedDay == 0) {
                [self dealWithSecond];
            } else {
                [self.thirdData addObjectsFromArray:@[@"0分",@"15分",@"30分",@"45分"]];
            }
        }
        //设置选中行数
        self.selectRow2 = row;
        self.selectRow3 = 0;
    } else {
        //设置选中行数
        self.selectRow3 = row;
    }
    [self returnPickerViewSelectString:pickerView];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    //设置分割线的颜色
    for(UIView *singleLine in pickerView.subviews) {
        if (singleLine.frame.size.height < 2) {
            singleLine.backgroundColor = [kCOLOR_lightLine colorWithAlphaComponent:0.3];
        }
    }
    //设置文字的属性（改变picker中字体的颜色大小）
    UILabel *reasonLabel = [UILabel new];
    reasonLabel.textAlignment = NSTextAlignmentCenter;
    reasonLabel.font = kFONT_mainFont(14);
    reasonLabel.textColor = KCOLOR_font_light;
    if (component == 0) {
        reasonLabel.text =  [self.firstData objectAtIndex:row];
        //改变选中行颜色（设置一个全局变量，在选择结束后获取到当前显示行，记录下来，刷新picker）
        if (self.selectRow1 == row) {
            //改变当前显示行的字体颜色，如果你愿意，也可以改变字体大小，状态
            reasonLabel.textColor = KCOLOR_mainColor;
        }
    } else if (component == 1 ){
        reasonLabel.text =  [self.secondData objectAtIndex:row];
        if (self.selectRow2 == row) {
            reasonLabel.textColor = KCOLOR_mainColor;
        }
    } else {
        reasonLabel.text =  [self.thirdData objectAtIndex:row];
        if (self.selectRow3 == row) {
            reasonLabel.textColor = KCOLOR_mainColor;
        }
    }
    return reasonLabel;
}

#pragma mark - TouchEvent


#pragma mark - Setter


#pragma mark - Getter

- (UIPickerView *)pickerview {
    if (!_pickerview) {
        _pickerview = [UIPickerView new];
        _pickerview.showsSelectionIndicator = YES;
        _pickerview.delegate = self;
        _pickerview.dataSource = self;
    }
    return _pickerview;
}

- (NSMutableArray *)firstData {
    if (!_firstData) {
        _firstData = [NSMutableArray array];
    }
    return _firstData;
}

- (NSMutableArray *)secondData {
    if (!_secondData) {
        _secondData = [NSMutableArray array];
    }
    return _secondData;
}

- (NSMutableArray *)thirdData {
    if (!_thirdData) {
        _thirdData = [NSMutableArray array];
    }
    return _thirdData;
}

@end
